//
//  ContactViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "ContactViewController.h"
#import "ContactDetailViewController.h"

@interface ContactViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *contactTableView;
@property (nonatomic, strong) NSMutableDictionary *contactDataSource;

@end

@implementation ContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self InitializeInterface];
}
- (void)InitializeInterface{
    [self.view addSubview:self.contactTableView];
}
#pragma mark - UITableViewDataSource/UITablewViewDelegate
//确定分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.contactDataSource.count;//字典键值对的个数
}
#pragma mark -- tabel detegate
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *keys = self.contactDataSource.allKeys;
    NSArray *sortKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
    NSString *letter = sortKeys[section];//获取按顺序的几个首字母
    return [self.contactDataSource[letter] count];//数据源数 = 行数
    
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentity = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: indentity ];
    
    if (! cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identity"];
    }
    NSArray *keys = self.contactDataSource.allKeys;
    NSArray *sortKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
    NSString *letter = sortKeys[indexPath.section];//获取按顺序的几个首字母
    cell.textLabel.text = self.contactDataSource[letter][indexPath.row];//取到第几组,再取第几个
    cell.imageView.image = [UIImage imageNamed:@"xingxing_yellow@2x"];
    cell.textLabel.font = [UIFont fontWithName:self.contactDataSource[letter][indexPath.row] size:20];
    return  cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点我 ");
    ContactDetailViewController *contactDetailVC = [[ContactDetailViewController alloc] init];
    [self.navigationController pushViewController:contactDetailVC animated:YES];
}
//边边排序
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    
    NSArray *keys = self.contactDataSource.allKeys;
    NSArray *sortKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
    NSString *letter = sortKeys[section];
    return letter;
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    NSArray *keys = self.contactDataSource.allKeys;
    NSArray *sortKeys = [keys sortedArrayUsingSelector:@selector(compare:)];
    
    return sortKeys;
}
#pragma mark - Getter
- (NSMutableDictionary *)contactDataSource
{
    if (! _contactDataSource) {
        _contactDataSource = [NSMutableDictionary dictionary];
        
        
        for (NSString *string in [UIFont familyNames]) {
            
            NSString *letter = [string substringToIndex:1];
            
            NSMutableArray *letterArray = _contactDataSource[letter];
            
            if (letterArray == nil) {
                
                letterArray = [NSMutableArray array];//如果没有,新建
            }
            [letterArray addObject:string];//字体放进数组
            [_contactDataSource setObject:letterArray forKey:letter];//把新数组放进字典
        }
    }
    return  _contactDataSource;
}

- (UITableView *)contactTableView{
    if (!_contactTableView) {
        _contactTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 375, 667-64) style:UITableViewStyleGrouped];
        _contactTableView.rowHeight = 80;
        _contactTableView.separatorColor = [UIColor lightGrayColor];
//        _contactTableView.showsHorizontalScrollIndicator = NO;
//        _contactTableView.showsVerticalScrollIndicator = NO;
//        [_contactTableView setSectionIndexBackgroundColor:[UIColor clearColor]];
//        [_contactTableView setSectionIndexColor:[UIColor grayColor]];
//        _contactTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];//去除多余空白行
        _contactTableView.delegate = self;
        _contactTableView.dataSource = self;
    }
    return _contactTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
